package main;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

/**
 * 
 * @author looser
 *
 *klasa trzyma parametry polaczenia takie jak host,user,password itp
 *
 */
public class ConnectionAttributes 
{
	public static String DB_ATTRIB_DBMS = "DBMS";
	public static String DB_ATTRIB_HOST = "host";
	public static String DB_ATTRIB_DRIVER = "driver";
	public static String DB_ATTRIB_PORT = "port";
	public static String DB_ATTRIB_USER = "user";
	public static String DB_ATTRIB_PASSWORD = "password";
	public static String DB_ATTRIB_ETYKIETA = "etykieta";
	public static String DB_ATTRIB_DATABASENAME = "databaseName";
	public static String DB_ATTRIB_CHARACTERENCODING = "characterEncoding";
	
	private String DBMS;
	private String host;
	private String driver;
	private String port;
	private String user;
	private String password;
	private String etykieta;
	private String databaseName;
	private String characterEncoding;
	
	public ConnectionAttributes(String DBMS,String host,String driver,String port,String databaseName,String user, String password,String etykieta )
	{
		this.DBMS=DBMS;
		this.host=host;
		this.driver=driver;
		this.port=port;
		this.user=user;
		this.password=password;
		this.etykieta=etykieta;
		this.databaseName=databaseName;
		//TODO poprawic, coby to skads wczytywac na modle tego powyzej
		characterEncoding = "utf-8";
	}
	
	public String getDBMS()
	{
		return DBMS;
	}
	public String getHost()
	{
		return host;
	}
	public String getDriver()
	{
		return driver;
	}
	public String getPort()
	{
		return port;
	}
	public String getUser()
	{
		return user;
	}
	public String getPassword()
	{
		 return password;
	}	
	public String getEtykieta()
	{
		 return etykieta;
	}
	public String getDatabaseName()
	{
		 return databaseName;
	}
	
	/****************************/
	public void setDBMS(String DBMS)
	{
		 this.DBMS=DBMS;
	}
	public void setHost(String host)
	{
		 this.host=host;
	}
	public void setDriver(String driver)
	{
		 this.driver=driver;
	}
	public void setPort(String port)
	{
		 this.port=port;
	}
	public void setUser(String user)
	{
		 this.user=user;
	}
	public void setPassword(String password)
	{
		this.password=password;
	}
	public void setEtykieta(String etykieta)
	{
		this.etykieta=etykieta;
	}
	public void setDatabaseName(String databaseName)
	{
		this.databaseName=databaseName;
	}
	
	/**********************/
	public Connection getConnection() throws ClassNotFoundException, SQLException
	{
		Class.forName(driver);
		String ods="/";
		if(DBMS.equals("sqlserver"))
			ods=";databaseName=";
		
		//TODO pozmieniane, zeby polsie znaki dzialaly
		String conString = "jdbc:" + DBMS + "://" + host + ":" + port + "/" + databaseName + "?" + DB_ATTRIB_USER + "=" + user + 
							"&" + DB_ATTRIB_PASSWORD + "=" + password + "&" + DB_ATTRIB_CHARACTERENCODING + "=" + characterEncoding;
		
		System.out.println(conString);
		
	//	return DriverManager.getConnection( "jdbc:"+DBMS+"://"+host+":"+port+ods+databaseName, user, password);
		
		return DriverManager.getConnection( conString);
	}
	
}
